using System;
using System.Diagnostics;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;

namespace NikCodes.SharePoint.ClientSide
{
    public class ScriptsCodeBehind : UserControl
    {
        protected Literal JQueryScript;
        protected Literal PrototypeScript;
        protected Literal JQueryUIScript;
        protected Literal ScriptaculousScript;
        protected Literal ExtScript;

        public ScriptsCodeBehind()
        {
            Load += OnLoad;
        }

        private void OnLoad(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                SPPropertyBag props = SPContext.Current.Web.Properties;

                JQueryScript.Visible = Utilities.IsPropertySet(props, "JQuery");
                SetJQueryUI(props);
                PrototypeScript.Visible = Utilities.IsPropertySet(props, "Prototype");
                SetScriptaculous(props);
                ExtScript.Visible = Utilities.IsPropertySet(props, "Ext");
            }
        }

        /// <summary>
        /// Sets the script code needed for jQueryUI
        /// </summary>
        /// <param name="props">The property bag of the site</param>
        private void SetJQueryUI(SPPropertyBag props)
        {
            if (Utilities.IsPropertySet(props, "JQueryUI"))
            {
                // We add the js
                JQueryUIScript.Text = "<script type=\"text/javascript\" src=\"/_layouts/ClientSide/jqueryui/jquery-ui-1.7.2.min.js\"></script>";

                // We add the selected theme
                JQueryUIScript.Text += "\r\n<link type=\"text/css\" href=\"/_layouts/ClientSide/jqueryui/themes/" + Utilities.GetProperty(props, "JQueryUITheme") + "/jquery-ui.css\" rel=\"stylesheet\" />";
            }
        }



        private void SetScriptaculous(SPPropertyBag props)
        {
            if (Utilities.IsPropertySet(props, "Scriptaculous"))
            {
                // We add the main scriptaculous js file
                ScriptaculousScript.Text =
                    "<script type=\"text/javascript\" src=\"/_layouts/ClientSide/scriptaculous/scriptaculous.js";

                // If there is any lib to add, we add them using ?=load= after the js file
                string addLibs = "";
                if (Utilities.IsPropertySet(props, "ScriptaculousBuilder"))
                    addLibs += "builder,";
                if (Utilities.IsPropertySet(props, "ScriptaculousEffects"))
                    addLibs += "effects,";
                if (Utilities.IsPropertySet(props, "ScriptaculousDragDrop"))
                    addLibs += "dragdrop,";
                if (Utilities.IsPropertySet(props, "ScriptaculousControls"))
                    addLibs += "controls,";
                if (Utilities.IsPropertySet(props, "ScriptaculousSlider"))
                    addLibs += "slider,";
                if (Utilities.IsPropertySet(props, "ScriptaculousSound"))
                    addLibs += "sound,";
                if (!string.IsNullOrEmpty(addLibs))
                    ScriptaculousScript.Text += "?load=" + addLibs.Substring(0, addLibs.Length - 1);

                ScriptaculousScript.Text += "\"></script>";
                
                // If there is Unit Test we include it separatly
                if (Utilities.IsPropertySet(props, "ScriptaculousUnitTest"))
                    ScriptaculousScript.Text += "\r\n<script type=\"text/javascript\" src=\"/_layouts/ClientSide/scriptaculous/unittest.js\"></script>";
            }
        }
    }
}
